import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/format_util.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/network_image_load/load_image.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/pages/site/widgets/power_rank_tab.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

import '../../../components/yt_text_style.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年03月27日 10:30
 */
class SitePowerRankWidget extends StatelessWidget {
  final String title;
  final PowerRankTabType type;
  final List<dynamic> rankList;
  final Function(PowerRankTabType) typeCallBack;

  const SitePowerRankWidget(
      {required this.title,
      required this.type,
      required this.rankList,
      required this.typeCallBack,
      super.key});

  @override
  Widget build(BuildContext context) {
    double itemHeight = 50.0;
    return Container(
      // color: Colors.red,
      // height: itemHeight * rankList.length + 95,
      child: Column(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(top: 16, left: 16.w, bottom: 10),
            child: Text(
              title,
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500),
            ),
          ),
          PowerRankTab(
            type,
            (PowerRankTabType tabType) {
              typeCallBack(tabType);
            },
            string1: ResKey.statisticsCharge.tr,
            string2: ResKey.statisticsDisCharge.tr,
            // string3: ''
            string3: ResKey.income.tr,
          ),
          Container(
            margin: EdgeInsets.only(right: 16.w, top: 10),
            height: itemHeight * rankList.length,
            child: ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              padding: const EdgeInsets.only(top: 10),
              itemBuilder: (BuildContext context, int index) {
                return _listItem(index, itemHeight);
              },
              itemCount: rankList.length,
            ),
          )
        ],
      ),
    );
  }

  _listItem(int index, double itemHeight) {
    var siteName = rankList[index]['siteName'] != null
        ? rankList[index]['siteName'].toString()
        : '--';
    Widget widget = Text((index + 1).toString(),
        style: ytTextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: Colour.labelTextColor,
        ));

    if (index == 0) {
      widget = Image.asset(
        'assets/images/site_rank_one.png',
        width: 22.w,
        height: 22.w,
      );
    } else if (index == 1) {
      widget = Image.asset(
        'assets/images/site_rank_two.png',
        width: 22.w,
        height: 22.w,
      );
    } else if (index == 2) {
      widget = Image.asset(
        'assets/images/site_rank_three.png',
        width: 22.w,
        height: 22.w,
      );
    }
    var value = rankList[index]['electricity'] != null
        ? rankList[index]['electricity'].toString()
        : '--';
    if (type == PowerRankTabType.profit) {
      value = rankList[index]['gain'] != null
          ? rankList[index]['gain'].toString()
          : '--';
      if (value.isNotEmpty && value != '--') {
        value = FormatUtil.commaSeparatedDigits(num.parse(value));
      }
    }
    double valueWidth = TextWidth.calculateTextSize(
        value,
        ytTextStyle(
          fontSize: 14,
          fontWeight: FontWeight.normal,
        ));
    var log = '';
    if (rankList[index]['photos'] != null &&
        rankList[index]['photos'].toString().isNotEmpty) {
      List list = rankList[index]['photos'].toString().split(',');
      if (list != null && list.isNotEmpty) {
        log = list.first;
      }
    }
    return Container(
        height: itemHeight,
        child: Column(children: [
          Row(children: [
            Container(
                alignment: Alignment.center,
                width: 20.w,
                height: 20.w,
                margin: EdgeInsets.only(
                  left: 16.w,
                  right: 8.w,
                ),
                child: widget),
            ClipOval(
              child: LoadImage(
                log,
                height: 26.w,
                width: 26.w,
                fit: BoxFit.cover,
                holderImg: 'assets/images/site_home_default',
              ),
            ),
            Gaps.hGap8,
            Container(
              width: ScreenUtil().screenWidth - valueWidth - 160.w,
              child: Text(siteName,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  style: ytTextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.normal,
                  )),
            ),
            Spacer(),
            Text.rich(
              TextSpan(
                children: [
                  TextSpan(
                      text: value,
                      style: dinTextStyle(
                          fontSize: 16, fontWeight: FontWeight.w500)),
                  TextSpan(
                      text: type == PowerRankTabType.profit
                          ? ResKey.yuan.tr
                          : "kWh",
                      style: ytTextStyle(fontSize: 11)),
                ],
              ),
            )
          ]),
          Gaps.vGap8,
          // Visibility(
          //     visible: index != rankList.length - 1,
          //     child: Container(height: 1,color: Colour.dividerColor,))
        ]));
  }
}
